import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/news_item_entity.dart';
import 'package:sports_new/entity/home/search/match_search_entity.dart';
import 'package:sports_new/entity/home/search/team_search_entity.dart';
import 'package:sports_new/entity/match/expert_views_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/expert/items/plan_item.dart';
import 'package:sports_new/view/home/news_cell.dart';
import 'package:sports_new/widgets/card.dart';

import '../../entity/home/search/league_search_entity.dart';
import '../../widgets/base/qkd_text.dart';

class SearchItem extends StatefulWidget {
  const SearchItem({
    super.key,
    required this.type,
    required this.data,
    required this.searchWord});

  final int type;
  final dynamic data;
  final String searchWord;

  @override
  State<SearchItem> createState() => _SearchItemState();
}

class _SearchItemState extends State<SearchItem> {

  @override
  Widget build(BuildContext context) {
    switch(widget.type){
      case 1:
        LeagueSearchEntity data = LeagueSearchEntity.fromJson(widget.data);
        return GestureDetector(
          onTap: () => Get.toNamed(
            Routes.userPage,
            arguments: data.id,parameters: {"type":"2"}
          ),
          child: common(data.logo ?? "", data.name ?? ""));
      case 2:
        return Block(
            cardStyle: const CardStyle(
                margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
                padding: EdgeInsets.symmetric(horizontal: 12)
            ),
            child: PlanItem(entity: PlanEntity.fromJson(widget.data))
        );
      case 3:
        TeamSearchEntity data = TeamSearchEntity.fromJson(widget.data);
        return GestureDetector(
          onTap: () => Get.toNamed(Routes.leagueDetail,
            arguments: data.id,parameters: {"type":"${(data.sportsId ?? 1)-1}"}),
          child: common(data.logo ?? "", data.name ?? "",sportsId: data.sportsId));
      case 4:
        TeamSearchEntity data = TeamSearchEntity.fromJson(widget.data);
        return GestureDetector(
          onTap: () => Get.toNamed(
            data.sportsId == 1?Routes.zqTeamDetail:Routes.lqTeamDetail,
            arguments: data.id
          ),
          child: common(data.logo ?? "", data.name ?? "",sportsId: data.sportsId));
      case 5:
        return match(MatchSearchEntity.fromJson(widget.data));
      case 6:
        var news = NewsItemEntity.fromJsonSearch(widget.data);
        return NewsItemCell(
          news,
          imgStyle: news.imgs == null?1:2,
        );
      default:
        return Container();
    }
  }

  Widget common(String logo,String name, {Widget? right,int?sportsId}){
    return Block(
      cardStyle: const CardStyle(
          margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
          padding: EdgeInsets.symmetric(horizontal: 12)
      ),
      child: SizedBox(
        height: 64,
        child: Row(
          children: [
            ClipOval(
              child: CachedNetworkImage(
                imageUrl: logo,
                width: 40,height: 40,
                fit: BoxFit.cover,
                placeholder: (context, url) => Container(),
                errorWidget: (context, url, error) => sportsId == null?Container():Image.asset(
                  Utils.getImgPath("${sportsId == 1?"zq":"lq"}_placeholder.png"),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Container(width: 8),
            QkdText(name,
              fontSize: 15,
              color: Colours.text,
              fontWeight: FontWeight.w500
            ).formHighLight(),
            const Spacer(),
            if(right != null)
            right
          ]
        ),
      ),
    );
  }

  Widget match(MatchSearchEntity entity){
    List<String> text = [entity.homeName ?? "","vs",entity.awayName ?? ""];
    if(entity.homeScore != null && entity.awayScore != null){
      text[1] = " ${entity.homeScore}:${entity.awayScore} ";
    }
    return GestureDetector(
      onTap: () => Get.toNamed(entity.sportsId == 1?Routes.zqMatchDetail:Routes.lqMatchDetail,arguments: entity.matchId),
      child: Block(
        cardStyle: const CardStyle(
          margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
          padding: EdgeInsets.symmetric(horizontal: 12)
        ),
        child: SizedBox(
          height: 64,
          child: Row(
            children: [
              QkdText(
                DateTime.tryParse(entity.matchTime ?? "")?.formatedString("MM/dd"),
                color: Colours.grey66,
              ),
              Expanded(
                child: QkdText(text.join(),
                  textAlign: TextAlign.end,
                    fontSize: 15,
                    color: Colours.text,
                    fontWeight: FontWeight.w500
                ).formHighLight(),
              ),
              Image.asset(Utils.getImgPath("arrow_right_36.png"),width: 12)
            ]
          ),
        ),
      ),
    );
  }
}
